/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:Candy"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using Candy.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using System;
using Candy.Infrastructure;

namespace Candy.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                // Create design time view services and models
                if (!SimpleIoc.Default.IsRegistered<IUserManager>())
                {
                    SimpleIoc.Default.Register<IUserManager, UserManager>();
                    SimpleIoc.Default.Register<INavigationService, SLNavigationService>();
                    SimpleIoc.Default.Register<IDataService, DummyDataService>();
                    SimpleIoc.Default.Register<IImportService, CandyImportService>();
                }
            }
            else
            {
                // Create run time view services and models
                SimpleIoc.Default.Register<IUserManager, UserManager>();
                SimpleIoc.Default.Register<INavigationService, SLNavigationService>();
                SimpleIoc.Default.Register<IDataService, CandySlDataService>();
                SimpleIoc.Default.Register<IImportService, CandyImportService>();
            }
            SimpleIoc.Default.Register<InstallViewModel>();
            SimpleIoc.Default.Register<LoginViewModel>();
            SimpleIoc.Default.Register<ProjectsViewModel>();
            SimpleIoc.Default.Register<RegistrationViewModel>();
            SimpleIoc.Default.Register<JobManagerViewModel>();
            SimpleIoc.Default.Register<CreateProjectViewModel>();
            SimpleIoc.Default.Register<SettingsViewModel>(true);
            SimpleIoc.Default.Register<HomeViewModel>();
            SimpleIoc.Default.Register<GenesViewModel>(true);
            SimpleIoc.Default.Register<FamiliesViewModel>(true);
            SimpleIoc.Default.Register<GenotypesViewModel>(true);
            SimpleIoc.Default.Register<PhylogeniesViewModel>(true);
            SimpleIoc.Default.Register<UpdateViewModel>();
            SimpleIoc.Default.Register<MoveToFamilyViewModel>();
            SimpleIoc.Default.Register<ImportViewModel>();
            SimpleIoc.Default.Register<ThemeTestViewModel>();

        }




        public InstallViewModel Install
        {
            get
            {
                return ServiceLocator.Current.GetInstance<InstallViewModel>();
            }
        }

        public LoginViewModel Login
        {
            get
            {
                return ServiceLocator.Current.GetInstance<LoginViewModel>();
            }
        }

        public ProjectsViewModel Projects
        {
            get
            {

                var vm = ServiceLocator.Current.GetInstance<ProjectsViewModel>();
                return vm;

            }
        }


        public RegistrationViewModel Registration
        {
            get
            {
                return ServiceLocator.Current.GetInstance<RegistrationViewModel>();
            }
        }

        public JobManagerViewModel JobManager
        {
            get
            {
                return ServiceLocator.Current.GetInstance<JobManagerViewModel>();
            }
        }

        public CreateProjectViewModel CreateProject
        {
            get
            {
                return ServiceLocator.Current.GetInstance<CreateProjectViewModel>();
            }
        }

        public SettingsViewModel Settings
        {
            get
            {
                return ServiceLocator.Current.GetInstance<SettingsViewModel>();
            }
        }

        public HomeViewModel Home
        {
            get
            {
                return ServiceLocator.Current.GetInstance<HomeViewModel>();
            }
        }


        public FamiliesViewModel Families
        {
            get
            {
                return ServiceLocator.Current.GetInstance<FamiliesViewModel>();
            }
        }

        public GenesViewModel Genes
        {
            get
            {
                return ServiceLocator.Current.GetInstance<GenesViewModel>();
            }
        }

        public GenotypesViewModel Genotypes
        {
            get
            {
                return ServiceLocator.Current.GetInstance<GenotypesViewModel>();
            }
        }

        public PhylogeniesViewModel Phylogenies
        {
            get
            {
                return ServiceLocator.Current.GetInstance<PhylogeniesViewModel>();
            }
        }

        public UpdateViewModel Update
        {
            get
            {
                return ServiceLocator.Current.GetInstance<UpdateViewModel>();
            }
        }

        public MoveToFamilyViewModel MoveToFamily
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MoveToFamilyViewModel>();
            }
        }


        public ImportViewModel Import
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ImportViewModel>();
            }
        }

        public ThemeTestViewModel ThemeTest
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ThemeTestViewModel>();
            }
        }

        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }

    public class ThemeTestViewModel : GalaSoft.MvvmLight.ViewModelBase
    {
        private string _testValidation;
        public string TestValidation
        {
            get
            {
                return _testValidation;
            }
            set
            {
                if (value != "Test")
                    throw new ArgumentException("Test is the only exceptable value for this field.");
                _testValidation = value;
                RaisePropertyChanged("TestValidation");
            }
        }


    }
}